package com.wdd.myplatform.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.wdd.myplatform.utils.Base64FileUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author wdd 2022/5/19
 */
@RestController
@RequestMapping("/paddleOcrController")
public class PaddleOcrController {

    private static String url = "http://120.53.224.43:8866/predict/ocr_system";

    @GetMapping("/testOcr")
    public static String testOcr() throws IOException {
        //从网上找一张带文字的图片
        //base64需要删除'data:image/jpg;base64,'
        String imageUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2F9f%2F62%2F0e%2F9f620e7809820b93e1e4a0aa9f0ecb2d.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655601437&t=a1e19933f75db0d10c639c6273bcfba7";
        String base64 = Base64FileUtils.getBase64ByImgUrl(imageUrl);
        JSONObject params = new JSONObject();
        List<String> base64List = Lists.newLinkedList();
        base64List.add(base64);
        params.put("images",base64List);
        String result = HttpUtil.post(url, params.toString());
        System.out.println(result);
        return result;
    }

    public static void main(String[] args) throws IOException {
        String testOcr = testOcr();
    }
}
